﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;

namespace Sandbox.Data.Mongo
{
    public static class Helper
    {
        private static MongoClient _client;
        private static MongoDatabase _database;

        static Helper()
        {
            _client = new MongoClient(ConfigurationManager.ConnectionStrings["MongoDB_UiTest"].ConnectionString);
            _database = _client.GetServer().GetDatabase("NiknakServicesUiTests");
        }

        public static MongoCollection<T> GetCollection<T>(string name)
        {
            return _database.GetCollection<T>(name) as MongoCollection<T>;
        }

        public static void InitClassMaps()
        {
            BsonSerializer.RegisterDiscriminatorConvention(typeof(Niknak.Services.TaskScheduler.Interfaces.DataModel.IAuditLog<AuditLogEntry>), new DiscriminatorConventions.AuditLog());
            BsonSerializer.RegisterDiscriminatorConvention(typeof(Niknak.Services.TaskScheduler.Interfaces.DataModel.IAuditLogEntry), new DiscriminatorConventions.AuditLogEntry());
            BsonSerializer.RegisterDiscriminatorConvention(typeof(Niknak.Services.TaskScheduler.Interfaces.DataModel.IScheduledTask), new DiscriminatorConventions.ScheduledTask());

            BsonClassMap.RegisterClassMap<AuditLog>(cm =>
            {
                cm.AutoMap();
                cm.MapProperty<Niknak.Services.TaskScheduler.Interfaces.DataModel.IAuditLogEntry[]>(c => (Niknak.Services.TaskScheduler.Interfaces.DataModel.IAuditLogEntry[])c.AuditLogEntries);
            });

            Debug.WriteLine("MongoDB classes mapped");
        }
    }
}
